<?php
class JsWidget extends CInputWidget
{
    public $scriptPath = null;
    public $codeMirrorOptions = array();
    
    private $_availableOptions = array(
        'stylesheet',
        'path',
        'parserfile',
        'basefiles',
        'iframeClass',
        'passDelay',
        'passTime',
        'continuousScanning',
        'autoMatchParens',
        'markParen',
        'unmarkParen',
        'saveFunction',
        'undoDepth',
        'onChange',
        'undoDelay',
        'width',
        'height',
        'disableSpellcheck',
        'textWrapping',
        'lineNumbers',
        'firstLineNumber',
        'lineNumberDelay',
        'lineNumberTime',
        'onLineNumberClick',
        'styleNumbers',
        'indentUnit',
        'tabMode',
        'enterMode',
        'electricChars',
        'reindentOnLoad',
        'readOnly',
        'domain',
        'noScriptCaching',
        'onLoad',
        'activeTokens',
        'onCursorActivity',
        'parserConfig',
        'content',
    );
    
    public function init()
    {
        foreach ($this->_availableOptions as $param) {
            if (isset($this->htmlOptions[$param])) {
                $this->codeMirrorOptions[$param] = $this->htmlOptions[$param];
                unset($this->htmlOptions[$param]);
            }
        }
        parent::init();
    }
    
    public function run()
    {
        echo CHtml::textArea($this->name, $this->value, $this->htmlOptions);
        $id = CHtml::getIdByName($this->name);
        $options = CMap::mergeArray(array(
            'height'=>'170px',
            'width'=>'500px',
            'continuousScanning'=>500,
            'lineNumbers'=>true,
            'parserfile'=>array('parsexml.js'),
            'parserfile'=>array('tokenizejavascript.js', 'parsejavascript.js'),
        ), $this->codeMirrorOptions);
        
        if (empty($this->scriptPath)) {
            $options['path'] = Yii::app()->theme->baseUrl.'/scripts/CodeMirror/js/';
            $options['stylesheet'] = array(Yii::app()->theme->baseUrl.'/scripts/CodeMirror/css/xmlcolors.css');
            $options['stylesheet'] = array(Yii::app()->theme->baseUrl.'/scripts/CodeMirror/css/jscolors.css');
        }
        
        $config = CJavaScript::encode($options);
        $script = "var editor = CodeMirror.fromTextArea('".$id."', {$config});";
        Yii::app()->clientScript->registerScript(__CLASS__.'#InitEditor'.$id, $script, CClientScript::POS_READY);
        $stylesheet = "
.CodeMirror-line-numbers {
    width: 2.2em;
    color: #aaa;
    background-color: #eee;
    text-align: right;
    padding-right: .3em;
    font-size: 10pt;
    font-family: monospace;
    padding-top: .4em;
    line-height: normal;
}
.CodeMirror-wrapping {
    border: 1px solid black;
    clear: both;
    margin-left: 210px
}
";
        Yii::app()->clientScript->registerCss(__CLASS__.'#InitEditor', $stylesheet);
        Yii::app()->clientScript->registerScriptFile(rtrim($options['path'], '/').'/codemirror.js');
    }
}